//
//  LYLCommentCell.m
//  ONE
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LYLCommentCell.h"


#import "LYLCommentModel.h"

NSString *const kLYLCommentCellID = @"LYLCommentCellID";

@interface LYLCommentCell ()

/** 用户视图 */
@property (nonatomic, strong) UIView *userView;

/** 用户头像 */
@property (nonatomic, strong) UIImageView *userAvatarView;

/** 用户名 */
@property (nonatomic, strong) UILabel *usernameLabel;

/** 日期 */
@property (nonatomic, strong) UILabel *dateLabel;

/** 点赞数 */
@property (nonatomic, strong) UILabel *praiseNumLabel;

/** 点赞按钮 */
@property (nonatomic, strong) UIButton *praiseButton;

/** 分数 */
@property (nonatomic, strong) UILabel *scoreLabel;

/** 官方图 */
@property (nonatomic, strong) UIImageView *officialPlotView;

/** 回复内容label */
@property (nonatomic, strong) UILabel *replyContentLabel;

/** 内容label */
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation LYLCommentCell

#pragma mark --准备重用
- (void)prepareForReuse
{
    [super prepareForReuse];
    _userAvatarView.image = nil;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createCommentCell];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCommentCell];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self createCommentCell];
    }
    return self;
}

#pragma mark --创建评论cell
- (void)createCommentCell
{
    if (_userView) {
        return;
    }
    
    _userView = [UIView new];
    _userView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_userView];
    [_userView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.contentView);
    }];
    
    _userAvatarView = [UIImageView new];
    _userAvatarView.backgroundColor = [UIColor whiteColor];
    _userAvatarView.contentMode = UIViewContentModeScaleAspectFill;
    _userAvatarView.layer.cornerRadius = 18;
    _userAvatarView.clipsToBounds = YES;
    [_userView addSubview:_userAvatarView];
    [_userAvatarView makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(36);
        make.left.top.equalTo(_userView).offset(12);
    }];
    
    _usernameLabel = [UILabel new];
    _usernameLabel.backgroundColor = [UIColor whiteColor];
    _usernameLabel.textColor = LYLAppThemeColor;
    _usernameLabel.font = FontWithSize(12);
    [_userView addSubview:_usernameLabel];
    [_usernameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_userAvatarView.right).offset(12);
        make.centerY.equalTo(_userAvatarView);
    }];
    
    _dateLabel = [UILabel new];
    _dateLabel.backgroundColor = [UIColor whiteColor];
    _dateLabel.textColor = [UIColor colorWithRed:0.77 green:0.77 blue:0.77 alpha:1.00];
    _dateLabel.font = FontWithSize(12);
    [_userView addSubview:_dateLabel];
    [_dateLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userAvatarView);
        make.right.equalTo(_userView).offset(-12);
    }];
    
    _praiseNumLabel = [UILabel new];
    _praiseNumLabel.backgroundColor = [UIColor whiteColor];
    _praiseNumLabel.textColor = _dateLabel.textColor;
    _praiseNumLabel.font = FontWithSize(12);
    [_userView addSubview:_praiseNumLabel];
    [_praiseNumLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dateLabel.bottom);
        make.right.equalTo(_dateLabel);
    }];
    
    _praiseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_praiseButton setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
    [_praiseButton setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateHighlighted];
    [_praiseButton addTarget:self action:@selector(didClickedLikeButton) forControlEvents:UIControlEventTouchUpInside];
    [_userView addSubview:_praiseButton];
    [_praiseButton makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(44);
        make.right.equalTo(_praiseNumLabel.left);
        make.top.equalTo(_dateLabel.bottom).offset(-12);
        make.bottom.equalTo(_userView);
    }];
    
    _scoreLabel = [UILabel new];
    _scoreLabel.textColor = LYLScoreTextColor;
    _scoreLabel.font = ScoreFontWithSize(20);
    [_userView addSubview:_scoreLabel];
    [_scoreLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_praiseButton);
        make.right.equalTo(_praiseButton.left).offset(-20);
    }];
    
    _officialPlotView = [UIImageView new];
    _officialPlotView.image = [UIImage imageNamed:@"officalPlot"];
    [_userView addSubview:_officialPlotView];
    [_officialPlotView makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(55, 49));
        make.centerY.equalTo(_usernameLabel);
        make.left.equalTo(_usernameLabel.right).offset(8);
    }];
    _officialPlotView.hidden = YES;
    
    _replyContentLabel = [UILabel new];
    _replyContentLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    _replyContentLabel.textColor = [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1.00];
    _replyContentLabel.font = FontWithSize(11);
    _replyContentLabel.numberOfLines = 0;
    [self.contentView addSubview:_replyContentLabel];
    [_replyContentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userView.bottom);
        make.left.equalTo(self.contentView).offset(60);
        make.right.equalTo(self.contentView).offset(-12);
    }];
    
    _contentLabel = [UILabel new];
    _contentLabel.backgroundColor = [UIColor whiteColor];
    _contentLabel.textColor = [UIColor blackColor];
    _contentLabel.font = FontWithSize(13);
    _contentLabel.numberOfLines = 0;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_replyContentLabel.bottom);
        make.left.right.equalTo(_replyContentLabel);
        make.bottom.equalTo(self.contentView).offset(-8);
    }];
}

#pragma mark --点赞按钮点击方法
- (void)didClickedLikeButton
{
    NSLog(@"~\(≧▽≦)/~");
}



#pragma mark --设置评论cell
- (void)configureCellForCommonWithComment:(LYLCommentModel *)comment atIndexPath:(NSIndexPath *)indexPath
{
    self.indexPath = indexPath;
    [_userAvatarView sd_setImageWithURL:[NSURL URLWithString:comment.userModel.web_url] placeholderImage:[UIImage imageNamed:@"personal"]];
    _usernameLabel.text = comment.userModel.user_name;
    
//    _dateLabel.text
    
    _praiseNumLabel.text = [@(comment.praisenum) stringValue];
    _scoreLabel.text = comment.score;
    _replyContentLabel.text = comment.quote;
    _contentLabel.text = comment.content;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
